<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/3/8
 * Time: 17:12
 */

namespace App\Rep;


use App\Models\JobType;
use App\Models\Product;
use App\Models\ProductCenter;
use App\Models\ProductJobType;
use Illuminate\Support\Facades\DB;
use PHPUnit\Exception;

class JobTypeRep
{

    /**
     * 获取工种列表
     * @param $params
     * @return mixed
     */
    public function getJobTypeListRep($params){
        $list = JobType::Search($params)->orderBy('id','desc')->paginate($params['per_page']??20);
        return $list;
    }

    /**
     * 下拉框
     * @param $params
     * @return mixed
     */
    public function getJobTypeListDropDownRep($params){
        $list = JobType::Search($params)->where('is_part',1)->orderBy('id','desc')->get();
        return $list;
    }

    /**
     * 添加工种
     * @param $params
     * @return JobType|array
     */
    public function createJobTypeRep($params){
        if(isset($params['id']) && !empty($params['id'])){
            $model = JobType::where('id',$params['id'])->first();
        }else{
            $model = JobType::where('job_type',$params['job_type'])->first();
            if($model){
                return ['code'=>1015,'data'=>[],'msg'=>'该工种名称已存在'];
            }
            $model = new JobType();
        }
        $model->job_type = $params['job_type'];
        $model->sort = $params['sort']??'1';
        $model->is_part = $params['is_part'];
        $model->is_multi = $params['is_multi'];
        $model->multi_order_num = $params['multi_order_num'];
        $model->is_restrict = $params['is_restrict'];
        $model->print_num = $params['print_num']??1;
        $model->save();
        return ['code'=>0,'data'=>$model,'msg'=>'success'];
    }

    /**
     * 删除工种
     * @param $params
     */
    public function delJobTypeRep($params){
        DB::beginTransaction();
        try {
            $product_id_arr = ProductJobType::where('job_type_id',$params['id'])->pluck('product_id')->toArray();
            ProductJobType::whereIn('product_id',$product_id_arr)->delete();
            $product_id_arrs = ProductCenter::where('job_type_id',$params['id'])->pluck('product_id')->toArray();
            ProductCenter::whereIn('product_id',$product_id_arrs)->delete();
            $model = JobType::where('id',$params['id'])->first();
            $model->is_del = $params['is_del'];
            $model->save();
            DB::commit();
            return ['code'=>0,'data'=>$model,'msg'=>'success'];
        }catch(Exception $exception){
            DB::rollBack();
            return ['code'=>2001,'data'=>[],'msg'=>$exception->getMessage()];
        }

    }

    /**根据工种ID获取员工列表**/
    public function getUserListByJobTypeIdRep($params){
        $jobType = JobType::where('id',$params['job_type_id'])->with(['user'=>function($q){
            $q->where('employee.is_del',2);
        }])->first();
        return ['code'=>0,'data'=>$jobType->user,'msg'=>'success'];
    }

    /**
     * 判断是否可以领取多个订单
     */
//    public function judgeIsMulti($job_type_id){
//        $is_multi = JobType::where('id',$job_type_id)->value('is_multi');
//        if($is_multi == 1){
//            return true;
//        }
//        return false;
//    }

    /**
     * 当前工序前的工序是否限制流程
     * 判断是否限制流程
     */
    public function judgeIsRestrict($job_type_id,$product_id){
        //$product = Product::where('id',$product_id)->with('job_type')->first();
        //p($product_id);
        //p($job_type_id);
        $product_job_type = ProductJobType::where('job_type_id',$job_type_id)->where('product_id',$product_id)->first();
        //p($product_job_type);
        $pid_arr = explode(',',$product_job_type->pid);
        //p($product_job_type->pid);
        foreach($pid_arr as $k=>$v){
            //p($v);
            $is_restrict = JobType::where('id',$v)->value('is_restrict');
            if($is_restrict == 1){
                return true;
                break;
            }
        }
        return false;
    }

    /**
     * 判断产品某一道工序是否完成
     */
    public function judgeIsOver($product_id,$job_type_id,$sort){
        $product_job_types = ProductJobType::where('product_id',$product_id)
            ->where('job_type_id',$job_type_id)
            ->pluck('status')->toArray();


    }

    /**
     * 获取产品某一工种的工序
     */
    public function getProductJobTypeSort($product_id,$job_type_id){
        return ProductJobType::where('product_id',$product_id)->where('job_type_id',$job_type_id)->value('sort');
    }



}
